package com.gem.ererlush;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DOMImpl {
private Document document;
private DocumentBuilder builder;
public void init(){
	DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
	try {
		builder=factory.newDocumentBuilder();
	} catch (ParserConfigurationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
public void createXML(String fileName) throws Exception{
	List<Food> food=DAOFactory.getIPerDAOInstance().fingAll();
	this.document=builder.newDocument();
	this.document.setXmlStandalone(true);
	this.document.setXmlVersion("1.0");
	Element root=this.document.createElement("root");
	Element Foods=this.document.createElement("Foods");
	for(int i=0;i<food.size();i++){
		Element Food=this.document.createElement("food");
		Element FoodId=this.document.createElement("FoodId");
		Element Name=this.document.createElement("Name");
		Element ChefId=this.document.createElement("ChefId");
		Element Time=this.document.createElement("Time");
		Element Info=this.document.createElement("Info");
		Element Collection=this.document.createElement("Collection");
		Element Attention=this.document.createElement("Attention");
		Element Discuss=this.document.createElement("Discuss");
		Element Image=this.document.createElement("Image");
		Element Come=this.document.createElement("Come");
		FoodId.appendChild(this.document.createTextNode(food.get(i).getFoodId()));
		Name.appendChild(this.document.createTextNode(food.get(i).getName()));
		ChefId.appendChild(this.document.createTextNode(food.get(i).getChefId()));
		Time.appendChild(this.document.createTextNode(food.get(i).getTime().toString()));
		Info.appendChild(this.document.createTextNode(food.get(i).getInfo()));
		Collection.appendChild(this.document.createTextNode(Integer.toString(food.get(i).getCollection())));
		Attention.appendChild(this.document.createTextNode(Integer.toString(food.get(i).getAttention())));
		Discuss.appendChild(this.document.createTextNode(Integer.toString(food.get(i).getDiscuss())));
		Image.appendChild(this.document.createTextNode(food.get(i).getImage()));
		Come.appendChild(this.document.createTextNode(food.get(i).getCome()));
		Food.appendChild(FoodId);
		Food.appendChild(Name);
		Food.appendChild(ChefId);
		Food.appendChild(Time);
		Food.appendChild(Info);
		Food.appendChild(Collection);
		Food.appendChild(Attention);
		Food.appendChild(Discuss);
		Food.appendChild(Image);
		Food.appendChild(Come);
		Foods.appendChild(Food);
	}
		root.appendChild(Foods);
		this.document.appendChild(root);
		try{
			TransformerFactory tf=TransformerFactory.newInstance();
			Transformer transformer=tf.newTransformer();
			DOMSource source=new DOMSource(document);
			transformer.setOutputProperty(OutputKeys.ENCODING, "GBK");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			PrintWriter pw=new PrintWriter(new FileOutputStream(fileName));
			StreamResult result = new StreamResult(pw);
			transformer.transform(source, result);
			System.out.println("XML保存成功");
		}catch(Exception e){
			throw e;
		}
		
		
		
		
	
}
public void parseXML(String fileName){
	try {
		this.document=builder.parse(fileName);
		NodeList root=document.getChildNodes();
		for(int i=0;i<root.getLength();i++){
			Node FoodsNode=root.item(i);
			NodeList Foods=FoodsNode.getChildNodes();
			for(int j=0;j<Foods.getLength();j++){
				Node FoodNode=Foods.item(j);
				NodeList Food = FoodNode.getChildNodes(); 
				for(int k=0;k<Food.getLength();k++){ 
					System.out.print(Food.item(k).getTextContent()); 
					} 
			}
		}
	} catch (SAXException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

public List<Food> getRecordList(String fileName){
	List<Food>  stuList=new ArrayList<Food>();
	try {
		this.document=builder.parse(fileName);
		NodeList root=this.document.getChildNodes();
		for(int i=0;i<root.getLength();i++){
			Node FoodsList=root.item(i);
		NodeList Foods=FoodsList.getChildNodes();
		for(int j=0;j<Foods.getLength();j++){
			Node FoodList=Foods.item(j);
			NodeList Food=FoodList.getChildNodes();
			for(int k=0;k<Food.getLength()-1;k++){
				if(k==(Food.getLength()-1)/2){
					break;
				}else{
					Node el=Food.item(k);
					Food food=elementToObject(el,k);
					stuList.add(food);
				}
				
			}
		}
		}
	} catch (SAXException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return stuList;
	
}


private Food elementToObject(Node el, int k) {
	// TODO Auto-generated method stub
	DateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
	Food food=new Food();
	food.setFoodId(this.document.getElementsByTagName("FoodId").item(k).getTextContent());
	food.setName(this.document.getElementsByTagName("Name").item(k).getTextContent());
	food.setChefId(this.document.getElementsByTagName("ChefId").item(k).getTextContent());
	try {
		food.setTime(format.parse(this.document.getElementsByTagName("Time").item(k).getTextContent()));
	} catch (DOMException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	food.setInfo(this.document.getElementsByTagName("Info").item(k).getTextContent());
	food.setCollection(Integer.parseInt(this.document.getElementsByTagName("Collection").item(k).getTextContent()));
	food.setAttention(Integer.parseInt(this.document.getElementsByTagName("Attention").item(k).getTextContent()));
	food.setDiscuss(Integer.parseInt(this.document.getElementsByTagName("Discuss").item(k).getTextContent()));
	food.setImage(this.document.getElementsByTagName("Image").item(k).getTextContent());
	food.setCome(this.document.getElementsByTagName("Come").item(k).getTextContent());
	return food;
}
}
